package com.ys.controller.api.inter.sys;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ys.dto.DtreeDto;
import com.ys.entity.sys.SysMenu;
import com.ys.service.sys.CommonApiService;
import com.ys.service.sys.SysMenuService;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;

@Controller
@RequestMapping("/api/inter/sysMenu")
public class ApiInterSysMenuController {
	
//	private static final Logger logger = Logger.getLogger(ApiAdminLoginController.class);
	
	@Autowired
	private SysMenuService sysMenuService;
	@Autowired
	private CommonApiService<SysMenu> commonApiService;
	
	@RequiresPermissions("sysMenu:view")
	@RequestMapping("/search")
    @ResponseBody
	public LayuiTableVoJA search(Model model, HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute SysMenu cond,
			Long roleId
			){
		
		return commonApiService.searchLay(cond);
	}
	
	@RequestMapping("/list")
	@ResponseBody
	public LayuiTableVoJA list(
    		Model model,
			HttpServletRequest request,
			HttpServletResponse response,
			String loginName,
			String pass,
			String code
			) {
		
		return sysMenuService.list();
	}
	
	@RequestMapping("/dtree")
    @ResponseBody
	public DtreeDto dtree(Model model, HttpServletRequest request, HttpServletResponse response,
			Integer maxLevel // 最大树等级
			){
		return sysMenuService.dtree(maxLevel);
	}
	
	/**
	 * 新增一级菜单
	 */
	@PostMapping("/addOneMenu")
    @ResponseBody
	public R addOneMenu(Model model, HttpServletRequest request, HttpServletResponse response,
			String name){
		return sysMenuService.addOneMenu(name);
	}
	
	/**
	 * 新增二级菜单
	 */
	@PostMapping("/addTwoMenu")
    @ResponseBody
	public R addTwoMenu(Model model, HttpServletRequest request, HttpServletResponse response,
			Long pid,
			String name){
		return sysMenuService.addTwoMenu(pid, name);
	}
	
	/**
	 * 新增三级菜单
	 */
	@PostMapping("/addThreeMenu")
    @ResponseBody
	public R addThreeMenu(Model model, HttpServletRequest request, HttpServletResponse response,
			Long pid, String name, String nameTable, Integer typeTemplet,
			Integer isCheckbox, Integer isShowNo, Integer isShowCount
			){
		
		System.out.println(nameTable);
		
		return sysMenuService.addThreeMenu(pid, name, nameTable, typeTemplet, isCheckbox, isShowNo, isShowCount);
	}
	
	/**
	 * 调整菜单顺序
	 */
	@PostMapping("/resort")
    @ResponseBody
	public R resort(Model model, HttpServletRequest request, HttpServletResponse response,
			String sysMenuArr){
		return sysMenuService.resort(sysMenuArr);
	}
	
	/**
	 * 菜单初始化-字段（总）
	 */
	@PostMapping("/threeMenuInitCol")
	@ResponseBody
	public R threeMenuInitCol(Model model, HttpServletRequest request, HttpServletResponse response,
			Long sysMenuId,
			String colArr){
		System.out.println(colArr);
		R retMsg;
		try {
			retMsg = sysMenuService.threeMenuInitCol(sysMenuId, colArr);
		} catch (Exception e) {
			retMsg = R.error("服务器正在开小差，请联系客服");
			e.printStackTrace();
		}
		return retMsg;
	}
	
	@RequestMapping("/deleteTrue")
    @ResponseBody
	public R deleteTrue(Model model, HttpServletRequest request, HttpServletResponse response,
			Long id){
		
		return sysMenuService.deleteTrue(id);
	}
	
	@RequestMapping("/editSysMenu")
    @ResponseBody
	public R editSysMenu(Model model, HttpServletRequest request, HttpServletResponse response,
			Long id,
			String name,
			String nameShiroAuth){
		
		return sysMenuService.editSysMenu(id, name, nameShiroAuth);
	}
	
	@RequestMapping("/editSysMenuFormBase")
    @ResponseBody
	public R editSysMenuFormBase(Model model, HttpServletRequest request, HttpServletResponse response,
			Long id,
			String pathPageAdd, String pathPageEdit, String pathCommitAdd, String pathCommitEdit){
		
		return sysMenuService.editSysMenuFormBase(id, pathPageAdd, pathPageEdit, pathCommitAdd, pathCommitEdit);
	}
	
}
